<!DOCTYPE html>

<html lang="en">
  <head>
    
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>命令行应用（Command Line Applications） &mdash; Phalcon 3.0.1 文档</title>
    <meta name="keywords" content="php, phalcon, phalcon php, php framework, faster php framework">
    <link rel="stylesheet" type="text/css" href="http://cdn.staticfile.org/twitter-bootstrap/3.3.6/css/bootstrap.min.css" />
    <link rel="stylesheet" type="text/css" href="/www/css/phalcon.min.css" />
    <link href='//fonts.googleapis.com/css?family=Open+Sans:700,400' rel='stylesheet' type='text/css'>
    <link href="//fonts.googleapis.com/css?family=Merriweather:400,700" rel="stylesheet" type="text/css" />
    <!--
    EUROPE <link href='//fonts.googleapis.com/css?family=Open+Sans:700,400&subset=latin-ext' rel='stylesheet' type='text/css'>
    GREEK <link href='//fonts.googleapis.com/css?family=Open+Sans:700,400&subset=greek-ext' rel='stylesheet' type='text/css'>
    RUSSIA <link href='//fonts.googleapis.com/css?family=Open+Sans:700,400&subset=cyrillic-ext,latin' rel='stylesheet' type='text/css'>
    -->

    <!-- HTML5 shim and Respond.js IE8 support of HTML5 elements and media queries -->
    <!--[if lt IE 9]>
      <script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
      <script src="https://oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->

    <link rel="stylesheet" href="../static/pygments.css" type="text/css" />
    <link rel="stylesheet" href="../static/docs.css" type="text/css" />
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '../',
        VERSION:     '3.0.1',
        COLLAPSE_MODINDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </script>

    <script src="http://cdn.staticfile.org/jquery/1.8.1/jquery.min.js"></script>
    <script type="text/javascript" src="../static/docs.js"></script>
    <link rel="shortcut icon" href="../static/favicon.ico"/>
    <link rel="top" title="Phalcon 3.0.1 文档" href="../index.html" />
    <link rel="next" title="队列（Queueing）" href="queue.html" />
    <link rel="prev" title="注释解析器（Annotations Parser）" href="annotations.html" /> 
  </head>
  <body>

<header class="page-header">
    <nav class="navbar" role="navigation">
        <div class="container">
            <div class="navbar-header">
                <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#main-menu-container">
                    <span class="sr-only">Toggle navigation</span><span class="icon-bar"></span><span class="icon-bar"></span><span class="icon-bar"></span>
                </button>
                <a class="navbar-brand phalcon-logo" href="/"><span itemprop="name" class="sr-only">Phalcon PHP</span></a>
            </div>

            <div class="collapse navbar-collapse navbar-right" id="main-menu-container">
                <ul class="nav navbar-nav main-menu">
                  <li class="first"><a href="//phalconphp.com/en/download" class="header-nav-link">Download</a></li>
                  <li><a href="//docs.phalconphp.com/en/latest/index.html" class="header-nav-link" target="_blank">Documentation</a></li>
                  <li><a href="//forum.phalconphp.com/" class="header-nav-link" target="_blank">Forum</a></li>
                  <li><a href="//blog.phalconphp.com/" class="header-nav-link" target="_blank">Blog</a></li>
                  <li><a href="//phalconist.com/" class="header-nav-link" target="_blank">Resources</a></li>
                  <li><a href="//phalconphp.com/en/about">About</a></li>
                  <li><div align="right">
                      <iframe src="https://ghbtns.com/github-btn.html?user=phalcon&amp;repo=cphalcon&amp;type=watch&amp;count=true&amp;size=large"
      allowtransparency="true" frameborder="0" scrolling="0" width="152px" height="30px"></iframe>
                    </div></li>
                </ul>
            </div>
        </div>
    </nav>
  </header>

<div class="heading">
    <div class="container">
        <div class="row">
            <h2>Documentation</h2>
        </div>
    </div>
</div>
    <div class="related">
      <ul>
        <li class="right" >
          <a href="../genindex.html" title="总目录"
             accesskey="I">索引</a></li>
        <li class="right" >
          <a href="queue.html" title="队列（Queueing）"
             accesskey="N">下一页</a> |</li>
        <li class="right" >
          <a href="annotations.html" title="注释解析器（Annotations Parser）"
             accesskey="P">上一页</a> |</li>
        <li><a href="//phalconphp.com">Home</a> &raquo;</li>
        <li><a href="../index.html">Phalcon 3.0.1 文档</a> &raquo;</li> 
      </ul>
    </div>  

      <table width="100%" align="center" cellpadding="0" cellspacing="0">
        <tr>
      <td class="primary-box" width="25%" valign="top">
            <div>
            <div id="searchbox" style="">
                <!--<form class="search" action="http://readthedocs.org/search/project/" method="get">
                  <input type="search" name="q" size="25" placeholder="Search">
                  <input type="submit" value="Go">
                  <input type="hidden" name="selected_facets" value="project:">
                </form>-->
                <div style="width:200px;padding:10px">
                  <gcse:searchbox-only></gcse:searchbox-only>
                </div>
            </div>
            </div>
            <div style="padding:5px;padding-left:10px">
              <div id="carbonads-container">
                <div class="carbonad"><div id="azcarbon"></div>
                <script async type="text/javascript" src="//cdn.carbonads.com/carbon.js?zoneid=1673&serve=C6AILKT&placement=phalconphpcom" id="_carbonads_js"></script>
                </div></div>
            </div>
            <h3><a href="../index.html">內容目录</a></h3>
            <ul>
<li><a class="reference internal" href="#">命令行应用（Command Line Applications）</a><ul>
<li><a class="reference internal" href="#structure">结构（Structure）</a></li>
<li><a class="reference internal" href="#creating-a-bootstrap">创建引导（Creating a Bootstrap）</a></li>
<li><a class="reference internal" href="#tasks">任务（Tasks）</a></li>
<li><a class="reference internal" href="#processing-action-parameters">处理动作参数（Processing action parameters）</a></li>
<li><a class="reference internal" href="#running-tasks-in-a-chain">链中运行任务（Running tasks in a chain）</a></li>
</ul>
</li>
</ul>

            <h4>上一个主题</h4>
            <p class="topless"><a href="annotations.html" title="上一章">&lt; 注释解析器（Annotations Parser）</a></p>
            <h4>下一个主题</h4>
            <p class="topless"><a href="queue.html" title="下一章">队列（Queueing） &gt;</a></p>
            <h3>本页</h3>
            <ul class="this-page-menu">
              <li><a href="../sources/reference/cli.md" rel="nofollow">显示源代码</a></li>
            </ul>
        </td>
          <td class="second-box" valign="top">
            <div class="document">
                <div class="documentwrapper">
                  <div class="bodywrapper">
                    <div class="body" >
                      
  <div class="section" id="command-line-applications">
<h1>命令行应用（Command Line Applications）<a class="headerlink" href="#command-line-applications" title="永久链接至标题">¶</a></h1>
<p>CLI应用即是运行在命令行窗体上的应用。 主要用来实现后台任务， 命令行工具等。</p>
<div class="section" id="structure">
<h2>结构（Structure）<a class="headerlink" href="#structure" title="永久链接至标题">¶</a></h2>
<p>最小结构的CLI程序如下：</p>
<ul class="simple">
<li>app/config/config.php</li>
<li>app/tasks/MainTask.php</li>
<li>app/cli.php &lt;&#8211; 主要启动文件</li>
</ul>
</div>
<div class="section" id="creating-a-bootstrap">
<h2>创建引导（Creating a Bootstrap）<a class="headerlink" href="#creating-a-bootstrap" title="永久链接至标题">¶</a></h2>
<p>普通的MVC程序中，启动文件用来启动整个应用。和web应用不同, 此处应用中我们使用cli.php来作为启动文件。</p>
<p>下面是一个简单的启动文件示例：</p>
<div class="highlight-php"><div class="highlight"><pre><span class="cp">&lt;?php</span>

<span class="k">use</span> <span class="nx">Phalcon\Di\FactoryDefault\Cli</span> <span class="k">as</span> <span class="nx">CliDI</span><span class="p">;</span>
<span class="k">use</span> <span class="nx">Phalcon\Cli\Console</span> <span class="k">as</span> <span class="nx">ConsoleApp</span><span class="p">;</span>
<span class="k">use</span> <span class="nx">Phalcon\Loader</span><span class="p">;</span>



<span class="c1">// 使用CLI工厂类作为默认的服务容器</span>
<span class="nv">$di</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">CliDI</span><span class="p">();</span>



<span class="sd">/**</span>
<span class="sd"> * 注册类自动加载器</span>
<span class="sd"> */</span>
<span class="nv">$loader</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">Loader</span><span class="p">();</span>

<span class="nv">$loader</span><span class="o">-&gt;</span><span class="na">registerDirs</span><span class="p">(</span>
    <span class="p">[</span>
        <span class="nx">__DIR__</span> <span class="o">.</span> <span class="s2">&quot;/tasks&quot;</span><span class="p">,</span>
    <span class="p">]</span>
<span class="p">);</span>

<span class="nv">$loader</span><span class="o">-&gt;</span><span class="na">register</span><span class="p">();</span>



<span class="c1">// 加载配置文件（如果存在）</span>

<span class="nv">$configFile</span> <span class="o">=</span> <span class="nx">__DIR__</span> <span class="o">.</span> <span class="s2">&quot;/config/config.php&quot;</span><span class="p">;</span>

<span class="k">if</span> <span class="p">(</span><span class="nb">is_readable</span><span class="p">(</span><span class="nv">$configFile</span><span class="p">))</span> <span class="p">{</span>
    <span class="nv">$config</span> <span class="o">=</span> <span class="k">include</span> <span class="nv">$configFile</span><span class="p">;</span>

    <span class="nv">$di</span><span class="o">-&gt;</span><span class="na">set</span><span class="p">(</span><span class="s2">&quot;config&quot;</span><span class="p">,</span> <span class="nv">$config</span><span class="p">);</span>
<span class="p">}</span>



<span class="c1">// 创建console应用</span>
<span class="nv">$console</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">ConsoleApp</span><span class="p">();</span>

<span class="nv">$console</span><span class="o">-&gt;</span><span class="na">setDI</span><span class="p">(</span><span class="nv">$di</span><span class="p">);</span>



<span class="sd">/**</span>
<span class="sd"> * 处理console应用参数</span>
<span class="sd"> */</span>
<span class="nv">$arguments</span> <span class="o">=</span> <span class="p">[];</span>

<span class="k">foreach</span> <span class="p">(</span><span class="nv">$argv</span> <span class="k">as</span> <span class="nv">$k</span> <span class="o">=&gt;</span> <span class="nv">$arg</span><span class="p">)</span> <span class="p">{</span>
    <span class="k">if</span> <span class="p">(</span><span class="nv">$k</span> <span class="o">==</span> <span class="mi">1</span><span class="p">)</span> <span class="p">{</span>
        <span class="nv">$arguments</span><span class="p">[</span><span class="s2">&quot;task&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="nv">$arg</span><span class="p">;</span>
    <span class="p">}</span> <span class="k">elseif</span> <span class="p">(</span><span class="nv">$k</span> <span class="o">==</span> <span class="mi">2</span><span class="p">)</span> <span class="p">{</span>
        <span class="nv">$arguments</span><span class="p">[</span><span class="s2">&quot;action&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="nv">$arg</span><span class="p">;</span>
    <span class="p">}</span> <span class="k">elseif</span> <span class="p">(</span><span class="nv">$k</span> <span class="o">&gt;=</span> <span class="mi">3</span><span class="p">)</span> <span class="p">{</span>
        <span class="nv">$arguments</span><span class="p">[</span><span class="s2">&quot;params&quot;</span><span class="p">][]</span> <span class="o">=</span> <span class="nv">$arg</span><span class="p">;</span>
    <span class="p">}</span>
<span class="p">}</span>



<span class="k">try</span> <span class="p">{</span>
    <span class="c1">// 处理参数</span>
    <span class="nv">$console</span><span class="o">-&gt;</span><span class="na">handle</span><span class="p">(</span><span class="nv">$arguments</span><span class="p">);</span>
<span class="p">}</span> <span class="k">catch</span> <span class="p">(</span><span class="nx">\Phalcon\Exception</span> <span class="nv">$e</span><span class="p">)</span> <span class="p">{</span>
    <span class="k">echo</span> <span class="nv">$e</span><span class="o">-&gt;</span><span class="na">getMessage</span><span class="p">();</span>

    <span class="k">exit</span><span class="p">(</span><span class="mi">255</span><span class="p">);</span>
<span class="p">}</span>
</pre></div>
</div>
<p>上面的代码可以使用如下方式执行：</p>
<div class="highlight-bash"><div class="highlight"><pre><span class="nv">$ </span>php app/cli.php

这样程序会直接执行默认的任务及默认动作.
</pre></div>
</div>
</div>
<div class="section" id="tasks">
<h2>任务（Tasks）<a class="headerlink" href="#tasks" title="永久链接至标题">¶</a></h2>
<p>这里的任务同于web应用中的控制器。 任一 CLI 应用程序都至少包含一个mainTask 及一个 mainAction， 每个任务至少有一个mainAction, 这样在使用者未明确的 指定action时 此mainAction就会执行。</p>
<p>下面即是一个mainTask的例子（ app/tasks/MainTask.php ）：</p>
<div class="highlight-php"><div class="highlight"><pre><span class="cp">&lt;?php</span>

<span class="k">use</span> <span class="nx">Phalcon\Cli\Task</span><span class="p">;</span>

<span class="k">class</span> <span class="nc">MainTask</span> <span class="k">extends</span> <span class="nx">Task</span>
<span class="p">{</span>
    <span class="k">public</span> <span class="k">function</span> <span class="nf">mainAction</span><span class="p">()</span>
    <span class="p">{</span>
        <span class="k">echo</span> <span class="s2">&quot;This is the default task and the default action&quot;</span> <span class="o">.</span> <span class="nx">PHP_EOL</span><span class="p">;</span>
    <span class="p">}</span>
<span class="p">}</span>
</pre></div>
</div>
</div>
<div class="section" id="processing-action-parameters">
<h2>处理动作参数（Processing action parameters）<a class="headerlink" href="#processing-action-parameters" title="永久链接至标题">¶</a></h2>
<p>CLI应用中， 开发者也可以在action中处理传递过来的参数， 下面的例子中已经对传递过来的参数进行了处理。</p>
<p>如果你使用下面的参数和动作运行应用程序:</p>
<div class="highlight-php"><div class="highlight"><pre><span class="cp">&lt;?php</span>

<span class="k">use</span> <span class="nx">Phalcon\Cli\Task</span><span class="p">;</span>

<span class="k">class</span> <span class="nc">MainTask</span> <span class="k">extends</span> <span class="nx">Task</span>
<span class="p">{</span>
    <span class="k">public</span> <span class="k">function</span> <span class="nf">mainAction</span><span class="p">()</span>
    <span class="p">{</span>
        <span class="k">echo</span> <span class="s2">&quot;This is the default task and the default action&quot;</span> <span class="o">.</span> <span class="nx">PHP_EOL</span><span class="p">;</span>
    <span class="p">}</span>

    <span class="sd">/**</span>
<span class="sd">     * @param array $params</span>
<span class="sd">     */</span>
    <span class="k">public</span> <span class="k">function</span> <span class="nf">testAction</span><span class="p">(</span><span class="k">array</span> <span class="nv">$params</span><span class="p">)</span>
    <span class="p">{</span>
        <span class="k">echo</span> <span class="nb">sprintf</span><span class="p">(</span>
            <span class="s2">&quot;hello %s&quot;</span><span class="p">,</span>
            <span class="nv">$params</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
        <span class="p">);</span>

        <span class="k">echo</span> <span class="nx">PHP_EOL</span><span class="p">;</span>

        <span class="k">echo</span> <span class="nb">sprintf</span><span class="p">(</span>
            <span class="s2">&quot;best regards, %s&quot;</span><span class="p">,</span>
            <span class="nv">$params</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span>
        <span class="p">);</span>

        <span class="k">echo</span> <span class="nx">PHP_EOL</span><span class="p">;</span>
    <span class="p">}</span>
<span class="p">}</span>
</pre></div>
</div>
<p>我们可以使用下面的命令行及参数执行程序：</p>
<div class="highlight-bash"><div class="highlight"><pre><span class="nv">$ </span>php app/cli.php main <span class="nb">test </span>world universe

hello world
best regards, universe
</pre></div>
</div>
</div>
<div class="section" id="running-tasks-in-a-chain">
<h2>链中运行任务（Running tasks in a chain）<a class="headerlink" href="#running-tasks-in-a-chain" title="永久链接至标题">¶</a></h2>
<p>CLI应用中可以在一个action中执行另一action. 要实现这个需要在 DI 中设置console.</p>
<div class="highlight-php"><div class="highlight"><pre><span class="cp">&lt;?php</span>

<span class="nv">$di</span><span class="o">-&gt;</span><span class="na">setShared</span><span class="p">(</span><span class="s2">&quot;console&quot;</span><span class="p">,</span> <span class="nv">$console</span><span class="p">);</span>

<span class="k">try</span> <span class="p">{</span>
    <span class="c1">// Handle incoming arguments</span>
    <span class="nv">$console</span><span class="o">-&gt;</span><span class="na">handle</span><span class="p">(</span><span class="nv">$arguments</span><span class="p">);</span>
<span class="p">}</span> <span class="k">catch</span> <span class="p">(</span><span class="nx">\Phalcon\Exception</span> <span class="nv">$e</span><span class="p">)</span> <span class="p">{</span>
    <span class="k">echo</span> <span class="nv">$e</span><span class="o">-&gt;</span><span class="na">getMessage</span><span class="p">();</span>

    <span class="k">exit</span><span class="p">(</span><span class="mi">255</span><span class="p">);</span>
<span class="p">}</span>
</pre></div>
</div>
<p>然后开发者即可在一个action中使用用其它的action了. 下面即是例子：</p>
<div class="highlight-php"><div class="highlight"><pre><span class="cp">&lt;?php</span>

<span class="k">use</span> <span class="nx">Phalcon\Cli\Task</span><span class="p">;</span>

<span class="k">class</span> <span class="nc">MainTask</span> <span class="k">extends</span> <span class="nx">Task</span>
<span class="p">{</span>
    <span class="k">public</span> <span class="k">function</span> <span class="nf">mainAction</span><span class="p">()</span>
    <span class="p">{</span>
        <span class="k">echo</span> <span class="s2">&quot;This is the default task and the default action&quot;</span> <span class="o">.</span> <span class="nx">PHP_EOL</span><span class="p">;</span>

        <span class="nv">$this</span><span class="o">-&gt;</span><span class="na">console</span><span class="o">-&gt;</span><span class="na">handle</span><span class="p">(</span>
            <span class="p">[</span>
                <span class="s2">&quot;task&quot;</span>   <span class="o">=&gt;</span> <span class="s2">&quot;main&quot;</span><span class="p">,</span>
                <span class="s2">&quot;action&quot;</span> <span class="o">=&gt;</span> <span class="s2">&quot;test&quot;</span><span class="p">,</span>
            <span class="p">]</span>
        <span class="p">);</span>
    <span class="p">}</span>

    <span class="k">public</span> <span class="k">function</span> <span class="nf">testAction</span><span class="p">()</span>
    <span class="p">{</span>
        <span class="k">echo</span> <span class="s2">&quot;I will get printed too!&quot;</span> <span class="o">.</span> <span class="nx">PHP_EOL</span><span class="p">;</span>
    <span class="p">}</span>
<span class="p">}</span>
</pre></div>
</div>
<p>当然， 通过扩展 <a class="reference internal" href="../api/Phalcon_Cli_Task.html"><em>Phalcon\Cli\Task</em></a> 来实现如上操作会是一个更好主意。</p>
</div>
</div>


                    </div>
                  </div>
                </div>
            </div>
          </td>
        </tr>
      </table>
    <div class="related">
      <ul>
        <li class="right" >
          <a href="../genindex.html" title="总目录"
             >索引</a></li>
        <li class="right" >
          <a href="queue.html" title="队列（Queueing）"
             >下一页</a> |</li>
        <li class="right" >
          <a href="annotations.html" title="注释解析器（Annotations Parser）"
             >上一页</a> |</li> 
      </ul>
    </div>

      <div class="prefooter">
  <div class="container">
      <div class="row">
          <div class="col-sm-3 text-right">
              <span>Follow along:</span>
          </div>
          <div class="col-sm-6 text-center">
              <a href="https://twitter.com/phalconphp" alt="Twitter" class="btn-social btn-social-twitter"><i class="icon-twitter"></i></a>
              <a href="https://www.facebook.com/pages/Phalcon-Framework/134230726685897" alt="Facebook" class="btn-social btn-social-facebook"><i class="icon-facebook"></i></a>
              <a href="https://plus.google.com/102376109340560896457" alt="Google+" class="btn-social btn-social-googleplus"><i class="icon-googleplus"></i></a>
              <a href="https://github.com/phalcon/cphalcon" alt="Github" class="btn-social btn-social-github"><i class="icon-github"></i></a>
          </div>
          <div class="col-sm-3">
          </div>
      </div>
  </div>

</div>
<footer class="footer">
    <div class="container">
        <div class="row">
            <div class="col-xs-4 col-sm-3">
                <h4>Download</h4>
                <ul>
                    <li><a href="/download">Installing Phalcon PHP</a></li>
                    <li><a href="http://docs.phalconphp.com/en/latest/index.html" class="header-nav-link" target="_blank">Documentation</a></li>
                    <li><a href="http://api.phalconphp.com">API</a></li>
                    <li><a href="http://docs.phalconphp.com/en/latest/reference/tutorial.html">Tutorial</a></li>
                    <li><a href="http://docs.phalconphp.com/en/latest/reference/tutorial.html#sample-applications">Sample Applications</a></li>
                </ul>
            </div>
            <div class="col-xs-4 col-sm-3">
                <h4>Community</h4>
                <ul>
                    <li><a href="http://forum.phalconphp.com/" class="header-nav-link" target="_blank">Forum</a></li>
                    <li><a href="https://github.com/phalcon/cphalcon">GitHub</a></li>
                    <li><a href="https://github.com/phalcon/cphalcon/issues">Issue Tracker</a></li>
                    <li><a href="http://stackoverflow.com/questions/tagged/phalcon">Stack Overflow</a></li>
                    <li><a href="/en/testimonials">Testimonials</a></li>
                    <li><a href="http://builtwith.phalconphp.com/">Built with Phalcon</a></li>
                    <li><a href="http://store.phalconphp.com/">Store</a></li>
                </ul>
            </div>
            <div class="col-xs-4 col-sm-2">
                <h4>About</h4>
                <ul>
                    <li><a class="link-black" href="http://blog.phalconphp.com/">Blog</a></li>
                    <li><a href="/en/about">About</a></li>
                    <li><a href="/en/team">Team</a></li>
                    <li><a href="/en/roadmap">Roadmap</a></li>
                    <li><a href="/en/donate">Donate</a></li>
                    <li><a href="/en/consulting">Consulting</a></li>
                    <li><a href="/en/hosting">Hosting</a></li>
                </ul>
            </div>
            <div id="license-spaccer" class="visible-xs"></div>
            <div id="license-wrapper" class="col-xs-12 col-sm-4">
                <p class="license">
                    Phalcon is an open-source PHP framework <br>built as a C-extension. It is available under the <br>
                    <a href="http://opensource.org/licenses/BSD-3-Clause" target="_blank">new BSD License</a>.
                    <br>
                    <br>
                    Except where otherwise noted, content on this site is licensed under the
                    <a href="http://creativecommons.org/licenses/by/3.0/">Creative Commons Attribution 3.0 License.</a>
                </p>
            </div>
        </div>
    </div>
</footer>

    </div>
    <script type="text/javascript">
    $(window).on("load", function(){
      var cx = '009733439235723428699:lh9ltjgvdz8';
      var gcse = document.createElement('script');
      gcse.type = 'text/javascript';
      gcse.async = true;
      gcse.src = (document.location.protocol == 'https:' ? 'https:' : 'http:') + '//www.google.com/cse/cse.js?cx=' + cx;
      var s = document.getElementsByTagName('script')[0];
      s.parentNode.insertBefore(gcse, s);
    });
    </script>

  </body>
</html>